package thread;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-03-04
 * Time:21:25
 */
public class SellTicket {
    public static void main(String[] args) {
        Window window = new Window();
        Thread thread1 = new Thread(window, "窗口一");
        Thread thread2 = new Thread(window, "窗口二");
        Thread thread3 = new Thread(window, "窗口三");
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

class Window implements Runnable {
    private static int ticketNum = 100;

    @Override
    public void run() {
        sellTicket();
    }

    public /*synchronized*/ void sellTicket() {
        synchronized (this) {
            while (true) {
                if (ticketNum <= 0) {
                    System.out.println("票已售尽");
                    break;
                }
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName() + "售出票号" + (ticketNum--));
            }
        }
    }
}
